import java.util.Stack;

class Solution {
    public String decodeString(String _s) {
        Stack<StringBuilder> st = new Stack<>();
        Stack<Integer> nums = new Stack<>();

        st.push(new StringBuilder());

        int i = 0, n = _s.length();
        char[] s = _s.toCharArray();

        while (i < n) {
            if (s[i] >= '0' && s[i] <= '9') {
                int tmp = 0;
                while (i < n && s[i] >= '0' && s[i] <= '9') {
                    tmp = tmp * 10 + (s[i] - '0');
                    i++;
                }
                nums.push(tmp);
            } else if (s[i] == '[') {
                i++;
                StringBuilder tmp = new StringBuilder();
                while (i < n && s[i] >= 'a' && s[i] <= 'z') {
                    tmp.append(s[i++]);
                }
                st.push(tmp);
            } else if (s[i] == ']') {
                StringBuilder tmp = st.pop();
                int k = nums.pop();
                for (int j = 0; j < k; j++) {
                    st.peek().append(tmp);
                }
                i++;
            } else {
                StringBuilder tmp = new StringBuilder();
                while (i < n && s[i] >= 'a' && s[i] <= 'z') {
                    tmp.append(s[i++]);
                }
                st.peek().append(tmp);
            }
        }
        return st.pop().toString();
    }
}